package com.cy.pj.sys.service.impl;

import com.cy.pj.common.bo.PageObject;
import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.CheckBox;
import com.cy.pj.common.pojo.SysRole;
import com.cy.pj.common.pojo.SysRoleMenuVo;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * @author Cx
 * @stage secondStageOfWeb
 * @creat 2020-11-14 15:32
 * 定义角色业务接口实现类 并添加角色业务数据分页查询操作的具体实现
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired(required = false)
    private SysRoleDao sysRoleDao;
    @Autowired(required = false)
    private SysRoleMenuDao sysRoleMenuDao;
    @Autowired(required = false)
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public List<CheckBox> findObjects() {
        return sysRoleDao.findObjects();
    }

    @Override
    public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {
        /* 对参数进行校验 */
        if (pageCurrent == null || pageCurrent < 1) throw new IllegalArgumentException("当前页码值无效");
        /* 查询总记录数并进行校验 */
        int rowCount = sysRoleDao.getRowCount(name);
        if (rowCount == 0) throw new ServiceException("没有找到对应记录");
        /* 查询当前页记录 */
        int pageSize = 10;
        int startIndex = (pageCurrent - 1) * pageSize;
        List<SysRole> records = sysRoleDao.findPageObjects(name, startIndex, pageSize);
        /* 对查询结果进行封装并返回 */
        return new PageObject<>(pageCurrent, pageSize, rowCount, records);
    }

    @Override
    public int deleteObject(Integer id) {
        /* 验证数据的合法性 */
        if (id == null || id <= 0) throw new IllegalArgumentException("请先选择");
        sysRoleMenuDao.deleteObjectsByRoleId(id);
        sysUserRoleDao.deleteObjectsByRoleId(id); /* 基于id删除关系数据 */
        int rows = sysRoleDao.deleteObject(id);
        if (rows == 0) throw new ServiceException("此纪录可能已经不存在");
        return rows; /* 返回结果 */
    }

    @Override
    public int saveObject(SysRole entity, Integer[] menuIds) {
        if (entity == null) throw new IllegalArgumentException("保存对象不允许为空");
        if (StringUtils.isEmpty(entity.getName())) throw new IllegalArgumentException("角色名不允许为空");
        if (menuIds == null || menuIds.length == 0) throw new ServiceException("必须为角色分配权限");
        int rows = sysRoleDao.insertObject(entity);
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return rows;
    }

    @Override
    public SysRoleMenuVo findObjectById(Integer id) {
        if (id == null || id <= 0) throw new IllegalArgumentException("id的值不合法");
        SysRoleMenuVo result = sysRoleDao.findObjectById(id);
        if (result == null) throw new ServiceException("此纪录已经不存在");
        return result;
    }

    @Override
    public int updateObject(SysRole entity, Integer[] menuIds) {
        //********************合法性验证
        if (entity == null) throw new IllegalArgumentException("更新的对象不能为空");
        if (entity.getId() == null) throw new IllegalArgumentException("id的值不能为空");
        if (StringUtils.isEmpty(entity.getName())) throw new IllegalArgumentException("角色名不能为空");
        if (menuIds == null || menuIds.length == 0) throw new IllegalArgumentException("必须为角色指定一个权限");
        //********************更新数据
        int rows = sysRoleDao.updateObject(entity);
        if (rows == 0) throw new ServiceException("对象可能已经不存在");
        sysRoleMenuDao.deleteObjectsByRoleId(entity.getId());
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return rows;
    }

}
